<?php

class Photo extends Controller {

    public function __construct() {
        parent::Controller();
        if (!$this->session->userdata('isAuthenticated')) {
            redirect(base_url().'/admin.php?c=auth&m=index');
        }
        $this->load->model('photomodel','photo');
    }

    public function index() {

        $this->load->library('pagination');

        $limit = 10;
        $offset = intval($this->input->get('per_page'));
        $total = $this->photo->p_count();

        $config['base_url'] = base_url() . '/admin.php?c=photo&m=index';
        $config['total_rows'] = $total;
        $config['per_page'] = $limit;
        $this->pagination->initialize($config);

        $data['title'] = '图片管理';
        $data['photos'] = $this->photo->p_list($limit, $offset);
        $data['page_links'] = $this->pagination->create_links();
        $this->load->view('photo/index', $data);
    }

    public function create() {
        $this->load->helper('form');
        $data['title'] = '上传图片';
        $this->load->view('photo/create', $data);
    }

    public function add() {
        $this->load->library('form_validation');
        $this->form_validation->set_rules('label', '标签', 'required');

        if ($this->form_validation->run() == false) {
            $this->load->view('photo/create');
        } else {
            $config['upload_path'] = './assets/images/uploads/';
            $config['allowed_types'] = 'gif|jpg|png';
            $config['max_size'] = '400';
            $config['max_width'] = '1024';
            $config['max_height'] = '768';
            $config['encrypt_name'] = true;

            $this->load->library('upload', $config);
            if (!$this->upload->do_upload('photo')) {
                $data['error'] = $this->upload->display_errors();
                $this->load->view('photo/create', $data);
            } else {
                $fileData = $this->upload->data();
                $photo['file_name'] = $fileData['file_name'];
                $photo['label'] = $this->input->post('label');
                $photo['created_at'] = date('Y-m-d H:i:s', time());

                if ($this->db->insert('photo', $photo)) {
                    $this->session->set_flashdata('notice', '图片上传成功！');
                    redirect(base_url().'/admin.php?c=photo&m=index');
                } else {
                    $data['error'] = '数据库错误！';
                    $this->load->view('photo/create', $data);
                }
            }
        }
    }

    public function delete() {
        $this->load->helper('file');
        $id = $this->getId();
        if ($row = $this->getRowById($id)) {
            $fileName = $row->file_name;
            $fullPath = './assets/images/uploads/' . $fileName;
            if (file_exists($fullPath)) {
                if (@unlink($fullPath)) {
                    if ($this->db->delete('photo', array('id' => $id))) {
                        $this->session->set_flashdata('notice', '图片删除成功！');
                    } else {
                        $this->session->set_flashdata('notice', '数据库错误！');
                    }
                } else {
                    $this->session->set_flashdata('notice', '文件删除失败！');
                }
            } else {
                show_404();
            }
        }
        redirect(base_url().'/admin.php?c=photo&m=index');
    }

    public function edit() {
        $this->load->helper('form');
        $id = $this->getId();
        if ($row = $this->getRowById($id)) {
            $data['title'] = '编辑图片';
            $data['id'] = $id;
            $data['label'] = $row->label;
            $this->load->view('photo/edit', $data);
        } else {
            show_404();
        }
    }

    public function update() {
        $id = intval($this->input->post('id'));
        if ($this->getRowById($id)) {
            $this->load->library('form_validation');
            $this->form_validation->set_rules('label', '标签', 'required');
            if ($this->form_validation->run() == false) {
                $data['title'] = '编辑图片';
                $data['id'] = $id;
                $this->load->view('photo/edit', $data);
            } else {
                $photo['label'] = $this->input->post('label');
                if ($this->db->update('photo', $photo, array('id' => $id))) {
                    $this->session->set_flashdata('notice', '图片修改成功！');
                    redirect(base_url().'/admin.php?c=photo&m=index');
                } else {
                    $data['error'] = '数据库错误！';
                    $this->load->view('photo/edit', $data);
                }
            }
        } else {
            show_404();
        }
    }

    protected function getId() {
        return intval($this->input->get('id'));
    }

    protected function getRowById($id) {
        $query = $this->db->query("select * from photo where id=$id limit 1");
        return ($query->num_rows() > 0) ? $query->row() : false;
    }

}